package com.hr.dept.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hr.dept.domain.Dept;
import com.hr.dept.service.Impl.DeptServiceImpl;
import com.hr.utils.BaseServlet;
import com.hr.utils.CommonUtils;
import com.hr.utils.PageBean;
/**
 * 部门Servlet服务层
 * @author 杨谦
 * @date 2015-11-2
 *
 */
public class DeptServlet extends BaseServlet {
	DeptServiceImpl deptServiceImpl = new DeptServiceImpl();
	/**
	 * 实现部门的查询分页
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryAllpage(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		/**
		 * 1、获取页面传递的PageCurrent
		 * 2、给定PageSize的值
		 * 3、使用PageCurrent和PageSize调用Service方法，得到PageBean，保存request域
		 * 4、转发到/admin/dept_info.jsp
		 */
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 5;//获取每页的记录数
		PageBean<Dept> pageBean = deptServiceImpl.queryAll_Page(PageCurrent, PageSize);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/admin/dept_info.jsp";
	}
	/**
	 * 获取当前的页面值
	 * @param request
	 * @return
	 */
	private int getPageCurrent(HttpServletRequest request){
		String value = request.getParameter("PageCurrent");
		if(value == null || value.trim().isEmpty()){
			return 1;
		} else{
			return Integer.parseInt(value);
		}
	}
	/**
	 * 截取url
	 * @param request
	 * @return
	 */
	public String getUrl(HttpServletRequest request){
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&PageCurrent=")) {
			int index = queryString.lastIndexOf("&PageCurrent=");
			queryString = queryString.substring(0, index);
		}
		
		return contextPath + servletPath + "?" + queryString;
	}
	/**
	 * 添加部门信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addDept(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		Dept dept = new Dept();
		dept.setDept_NAME(request.getParameter("dept_Name"));
		dept.setDept_ID(CommonUtils.uuid());
		dept.setDept_STATE(request.getParameter("dept_state"));
		dept.setDept_DESCRIBLE(request.getParameter("dept_Describle"));
		deptServiceImpl.addDept(dept);
		request.setAttribute("msg", "添加成功！");
		return "f:/DeptServlet?method=queryAllpage";
	}
	/**
	 * 修改部门信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Dept dept = new Dept();
		dept.setDept_ID(request.getParameter("dept_ID"));
		dept.setDept_NAME(request.getParameter("dept_Name"));
		dept.setDept_STATE(request.getParameter("dept_state"));
		dept.setDept_DESCRIBLE(request.getParameter("dept_Describle"));
		deptServiceImpl.updateDept(dept);
		request.setAttribute("msg", "修改部门成功！");
		return "f:/DeptServlet?method=queryAllpage";
	}
	/**
	 * 根据部门编号删除部门
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String dept_ID = request.getParameter("dept_ID");
		deptServiceImpl.deleteDept(dept_ID);
		request.setAttribute("msg", "删除成功！");
		return "f:/DeptServlet?method=queryAllpage";
	}
	/**
	 * 回显信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String showDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String dept_ID = request.getParameter("dept_ID");
		Dept dept = deptServiceImpl.findDeptByID(dept_ID);
		request.setAttribute("dept", dept);
		return "f:/admin/dept_update.jsp";
	}
}
